package com.fengdi.controller;

import com.fengdi.pojo.Category;
import com.fengdi.pojo.Result;
import com.fengdi.service.serviceInterface.CategoryService;
import com.fengdi.utils.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * @author FengDi
     * @Description 新增文章分类
     * @param category: 分类对象
     * @return 新建结果
     * */
    @PostMapping("/add")
    public Result<?> addCategory(@RequestBody Category category) {
        return this.categoryService.addCategory(category);
    }

    /**
     * @author FengDi
     * @Description 获取当前用户的所有文章分类
     * @param
     * @return 请求结果中携带分类列表数据
     * */
    @GetMapping
    public Result<List<Category>> getCategory() {
        return this.categoryService.getCategories(UserThreadLocalUtil.getUserID());
    }

    /**
     * @author FengDi
     * @Description 获取文章分类详情
     * @param id: 分类ID
     * @return  分类详细信息
     * */
    @GetMapping("/detail")
    public Result<Category> getCategoryDetail(Integer id) {
        return this.categoryService.getCategoryDetails(id);
    }

    /**
     * @author FengDi
     * @Description 更新文章分类
     * @param category: 更新信息
     * @return 更新是否成功
     * */
    @PutMapping("")
    public Result<?> updateCategory(@RequestBody @Validated Category category) {
        return this.categoryService.updateCategory(category);
    }

    /**
     * @author FengDi
     * @Description  删除文章分类
     * @param   id: 分类id
     * @return 删除结果
     * */
    @DeleteMapping("")
    public Result<?> deleteCategory(@Validated Integer id) {
        return this.categoryService.deleteCategory(id);
    }
}
